package com.sitanyun.merchant.guide.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.base.utils.NetworkStateUtils;
import com.sitanyun.merchant.guide.bean.SwiShopsBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.Aes;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import androidx.appcompat.app.AppCompatActivity;
import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 启动页
 */
public class SplashsActivity extends AppCompatActivity  {
    private Handler handler;
    private static final String TAG = "OpenClickActivity";
    /**消息Id**/
    private static final String KEY_MSGID = "msg_id";
    /**该通知的下发通道**/
    private static final String KEY_WHICH_PUSH_SDK = "rom_type";
    /**通知标题**/
    private static final String KEY_TITLE = "n_title";
    /**通知内容**/
    private static final String KEY_CONTENT = "n_content";
    /**通知附加字段**/
    private static final String KEY_EXTRAS = "n_extras";
    private TextView mTextView;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //判断是否第一次进入
     //   handleOpenClick();
        initOkhttp();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
   //刷新token
    private void initOkhttp() {
        handler = new Handler();

        //延迟 SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                if (!NetworkStateUtils.isNetworkConnected(SplashsActivity.this)) {
                    ToastUtil.showToast(SplashsActivity.this, "网络异常,请稍后重试");
                    startActivity(new Intent(SplashsActivity.this, LoginActivity.class));
                    finish();
                    return;
                }
                if (SharedPreferenceUtil.getStringData("token").isEmpty()){
                    startActivity(new Intent(SplashsActivity.this, LoginActivity.class));
                    //未登陆
                    finish();
                }else {
                    //已经登陆
                    Aes mAes = new Aes();
                    try {
                        String secret=  SharedPreferenceUtil.getStringData("cid")
                                +","+SharedPreferenceUtil.getStringData("nodeid")+","
                                +SharedPreferenceUtil.getStringData("staffid")+","
                                +SharedPreferenceUtil.getStringData("userid");
                        byte[]  secrets = secret.getBytes("UTF8");
                        String   secretaec = mAes.encrypt(secrets);//加密

                        HashMap<String, String> hashMap = new HashMap<String, String>();
                        hashMap.put("staffId",SharedPreferenceUtil.getStringData("staffid"));
                        hashMap.put("userId", SharedPreferenceUtil.getStringData("userid"));
                        hashMap.put("clientId",  SharedPreferenceUtil.getStringData("cid"));//
                        hashMap.put("nodeId", SharedPreferenceUtil.getStringData("nodeid") );//

                        hashMap.put("secret",  secretaec);//

                        OkHttpUtils.postString()
                                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                                .addHeader("Content-Type", "application/json")
                                //   .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                                .url(Urls.manual)
                                .content(new Gson().toJson(hashMap))
                                .build().execute(new StringCallbacks() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                startActivity(new Intent(SplashsActivity.this, LoginActivity.class));
                              finish();
                            }
                            @Override
                            public void onResponse(String response, int id) {
                                SwiShopsBean loginBean= new Gson().fromJson(response, SwiShopsBean.class);
                                if (loginBean.getCode() == 0) {
                                    SharedPreferenceUtil.SaveData("tonodename", loginBean.getTop_node_name()+"");
                                    SharedPreferenceUtil.SaveData("userid", loginBean.getUser_id());
                                    SharedPreferenceUtil.SaveData("nodename", loginBean.getNode_name());
                                    SharedPreferenceUtil.SaveData("token", loginBean.getAccess_token());
                                    SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
                                    SharedPreferenceUtil.SaveData("clientid", loginBean.getS_client_id());
                                    SharedPreferenceUtil.SaveData("nodeid",loginBean.getNode_id());
                                    SharedPreferenceUtil.SaveData("tonodeid", loginBean.getTop_node_id());
                                    SharedPreferenceUtil.SaveData("sxtoken", loginBean.getRefresh_token());
                                    SharedPreferenceUtil.SaveData("cid", loginBean.getS_client_id());
                                    SharedPreferenceUtil.SaveData("staffname", loginBean.getStaff_name());
                                 //   Log.e("LOGS","111");
                                    Intent intent = getIntent();
                                    if (null != intent) {
                                        Bundle bundle = getIntent().getExtras();
                                        String title = null;
                                        content = null;
                                        if (bundle != null) {
                                            //title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
                                            content = bundle.getString(JPushInterface.EXTRA_EXTRA);
                                        }
                                    }
                                    startActivity(new Intent(SplashsActivity.this,MainActivity.class)
                                            .putExtra("cid",content+"")
                                    );
                                    finish();

                                } else {
                                 //   Log.e("LOGS","222");

                                    startActivity(new Intent(SplashsActivity.this,LoginActivity.class));
                                       //     .putExtra("cid",getIntent().getSerializableExtra("cid")));
                                    finish();
                                }
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }
        }, 3000);
    }
}
